﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TvControl;
using TvDatabase;
using TvLibrary.Log;
using TvEngine;
using TvLibrary.Interfaces;
using SetupTv;
using System.IO;

namespace TvServerPlugin
{
    public class TV4HomeWebUIHost : ITvServerPlugin
    {
        private Cassini.Server webServer;

        #region ITvServerPlugin Members

        public string Name
        {
            get { return "TV 4 Home WebUI Host"; }
        }

        public string Version
        {
            get { return "0.1.0.0"; }
        }

        public string Author
        {
            get { return "TV 4 Home"; }
        }

        public bool MasterOnly
        {
            get { return false; }
        }

        public void Start(IController controller)
        {
            System.Threading.Thread th = new System.Threading.Thread(StartWebServer);
            th.Start();
        }

        private void StartWebServer()
        {
            Settings.LoadSettings();
            if (Settings.httpPort > 0)
            {
                try
                {
                    if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\"))
                    {
                        try
                        {
                            Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\");
                        }
                        catch (Exception ex)
                        {

                            Log.Error("TV4Home Error in Create TV4Home Folder" + ex.StackTrace.ToString());
                        }
                    }
                    if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\htdocs\\"))
                    {
                        try
                        {
                            Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\htdocs\\");
                        }
                        catch (Exception ex)
                        {

                            Log.Error("TV4Home Error in Create TV4Home\\TVServerPlugin folder" + ex.StackTrace.ToString());
                        }
                    }
                    
                    this.webServer = new Cassini.Server(Settings.httpPort, "/",  Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\htdocs\\");
                    this.webServer.Start();
                }
                catch (Exception ex)
                {
                    Log.Error("TV4Home: Exception raised while starting WebServer:" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
                    return;
                }
            }
        
        }
        private void StopWebServer()
        {
            if (webServer != null)
            {
                Log.Info("plugin: TV4Home webserver stop");
                webServer.Stop();
                webServer = null;
            }
        
        }

        public void StartedAll()
        {            
        }

        public void Stop()
        {
            StopWebServer();
        }

        public SetupTv.SectionSettings Setup
        {
            get { return new TV4HomeWebUIHostSetup(); }
        }

        #endregion
    }
}
